﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Service
{
    public class MyRequest
    {
        private EKAASBIMEntities dbContext;
        public MyRequest()
        {
            dbContext = new EKAASBIMEntities();
        }

        public List<requirementRecords> getRecords(string user)
        {
            return dbContext.requirementRecords.Where(o => o.employee == user).ToList();
        }

        public List<requirementRecords> getRecordsBystatus(int status)
        {
            //           return dbContext.requirementRecords.Where(o=>o.status==Sta).ToList();
            return dbContext.requirementRecords.Where(o => o.status == status).ToList();
        }

        public void insert(requirementRecords item, out bool b, string userName)
        {
            b = false;
            item.date = DateTime.Now;
            item.employee = userName;
            item.status = 0;
            if (dbContext.requirementRecords.SingleOrDefault(o => o.ID == item.ID) != null)
            {
                b = true;
                return;
            }
            dbContext.requirementRecords.AddObject(item);
            dbContext.SaveChanges();
        }

        public List<requirementRecords> delete(string[] items)
        {
            List<requirementRecords> records = new List<requirementRecords>();
            foreach (string item in items)
            {
                int id = default(int);
                int.TryParse(item, out id);
                var d = dbContext.requirementRecords.SingleOrDefault(o => o.ID == id);
                if (d.status != 0 && d.status!=3)
                {
                    continue;
                }
                if (d != null)
                {
                    records.Add(d);
                    dbContext.DeleteObject(d);
                }
            }
            dbContext.SaveChanges();
            return records;
        }

        public void update(requirementRecords item)
        {
            requirementRecords oldItem = dbContext.requirementRecords.SingleOrDefault(o => o.ID == item.ID);
            if (oldItem == null || oldItem.status == 1 || oldItem.status == 2)
            {
                return;
            }
            oldItem.date = DateTime.Now;
            oldItem.productName = item.productName;
            oldItem.number = item.number;
            oldItem.description = item.description;
            oldItem.status = 0;

            dbContext.SaveChanges();
        }
    }
}
